////////////////////////////////////////////////////////////////
// filename: list.cpp
// description: code for our linked list
// autor: Jay Tomten (jaytomten@gmail.com)
// notes:

// namespace

// defines

// includes
#include <iostream>
#include "list.h"

// globals

////////////////////////////////////////////////////////////////
// constructor
List::List()
    {
    front = NULL;
    back = NULL;
    }

////////////////////////////////////////////////////////////////
// destructor
List::~List()
    {

    while (back != NULL)
        {
        Remove(back);
        }

    }

////////////////////////////////////////////////////////////////
// InsertBefore() - insert a node before n
void List::InsertBefore(Object *a, Node *n)
    {

    Node *newNode = new Node();
    newNode->prev = n->prev;
    newNode->next = n;
    newNode->o = a;
    if(n->prev == NULL) this->front = newNode;
    n->prev = newNode;

    }

////////////////////////////////////////////////////////////////
// InsertFront() - insert a node before the front node
void List::InsertFront(Object *a)
    {

    if  (this->front == NULL)   // if first object in list
        {
        Node *newNode;
        newNode = new Node();
        this->front = newNode;
        this->back = newNode;
        newNode->prev = NULL;
        newNode->next = NULL;
        newNode->o = a;
        }
    else InsertBefore(a, this->front);

    }

////////////////////////////////////////////////////////////////
// InsertAfter() - insert a node after n
void List::InsertAfter(Object *a, Node *n)
    {

    Node *newNode = new Node();
    newNode->next= n->next ;
    newNode->prev  = n;
    newNode->o = a;
    if(n->next == NULL) this->back = newNode;
    n->next = newNode;

    }

////////////////////////////////////////////////////////////////
// InsertBack() - insert a node after the last node
void List::InsertBack(Object *a)
    {

    if(this->back == NULL) InsertFront(a);
    else InsertAfter(a, this->back);

    }

////////////////////////////////////////////////////////////////
// Remove() - remove a node
void List::Remove(Node *n)
    {

    if  (this->front == NULL && this->back == NULL)   // are we empty?
        {
        cout << "Trying to remove a node that does not exist!\n";
        }
    else if (this->front == this->back)     // is there just one node?
        {
        //cout << "Removing the only node in list.\n";
        this->front = NULL;
        this->back  = NULL;
        }
    else     // more than one node?
        {
        if  (n == this->front)
            {
            //cout << "Removing the front node in list.\n";
            this->front = this->front->next;
            this->front->prev = NULL;
            }
        else if(n == this->back)
            {
            //cout << "Removing the back node in list.\n";
            this->back = this->back->prev;
            this->back->next = NULL;
            }
        else
            {
            //cout << "Removing a middle node in list.\n";
            n->prev->next = n->next;
            n->next->prev = n->prev;
            }
        }
     delete &n;

    }


